﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;

namespace sistemaAcademicoFinanceiroEscolas.alunosInsert
{
    public partial class Insert : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            DynamicDataManager1.RegisterControl(DetailsView1);
            Session["jadefiniufoco"] = "Nao";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            table = DetailsDataSource.GetTable();
            Title = table.DisplayName;
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == DataControlCommands.CancelCommandName)
            {
                Response.Redirect(table.ListActionPath);
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {
                SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqlc.Open();
                //string select1 = "select MAX(codigoAluno) AS codigoAluno from aluno";



                string select1 = "select MAX(codigoAluno) AS codigoAluno from aluno";
                SqlCommand cmd1 = new SqlCommand(select1, sqlc);
                SqlDataReader dr1 = cmd1.ExecuteReader();

                dr1.Read();


                string codigoAlunoMax = dr1["codigoAluno"].ToString();

                dr1.Close();


                string select = "select nomeAluno from aluno where codigoAluno = " + codigoAlunoMax;
                SqlCommand cmd = new SqlCommand(select, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                dr.Read();

                string nomeAlunoMax = dr["nomeAluno"].ToString();


                dr.Close();

                sqlc.Close();




                string sqls = "select * from aluno";
                System.Data.SqlClient.SqlConnection sqle = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqle.Open();
                System.Data.SqlClient.SqlCommand cmdListar = new System.Data.SqlClient.SqlCommand(sqls, sqle);
                System.Data.SqlClient.SqlDataReader drListar = cmdListar.ExecuteReader();

                string nomeAluno = "";
                string codigoAluno = "";

                if (drListar.HasRows)
                {
                    while (drListar.Read())
                    {
                        codigoAluno = drListar["codigoAluno"].ToString();
                        nomeAluno = drListar["nomeAluno"].ToString();

                        nomeAlunoMax = nomeAlunoMax.ToUpper();
                        nomeAluno = nomeAluno.ToUpper();

                        if (codigoAluno != codigoAlunoMax && nomeAlunoMax.Equals(nomeAluno))
                        {
                            SqlConnection sqlDelMaquina = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                            sqlDelMaquina.Open();

                            string sqlDeletar = "delete from aluno where codigoAluno = " + codigoAlunoMax + "";

                            SqlCommand deletarDetalhe = new SqlCommand(sqlDeletar, sqlDelMaquina);
                            deletarDetalhe.ExecuteNonQuery();

                            sqlDelMaquina.Close();

                            //Response.Write("codigoAluno: " + codigoAluno + "<br>");
                            //Response.Write("codigoAlunoMax: " + codigoAlunoMax + "<br>");
                            //Response.Write("nomeAlunoMax: " + nomeAlunoMax + "<br>");
                            //Response.Write("nomeAluno: " + nomeAluno + "<br>");


                            Response.Redirect("Insert.aspx?informacao=sim");
                        }
                    }
                }

                cmdListar.Dispose();
                drListar.Close();
                Response.Redirect(table.ListActionPath);



            }
        }
    }
}
